package qlsl.androiddesign.http.service.subservice;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.constant.MessageConstant;
import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 论坛模块 <br/>
 * 
 */
public class ForumService extends BaseService {

	private static String className = getClassName(ForumService.class);

	/**
	 * 查询帖子列表<br/>
	 * 
	 */
	public static void queryPostingList(final int page, final String section, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询帖子");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryPostingList");
		new HandlerThread(className, "queryPostingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist").addParam("orderby", 0)
							.addParam("section", section).addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE).addParam("city", Constant.city)
							.removeUserParam().post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询帖子列表[Tab3]<br/>
	 * 
	 */
	public static void queryNewPostingList(final int page, final String section, final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandlerWithoutShowing(functionView, listener, className, "queryPostingList");
		new HandlerThread(className, "queryPostingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist").addParam("orderby", 0)
							.addParam("section", section).addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE).addParam("city", Constant.city)
							.removeUserParam().post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询帖子列表<br/>
	 * 
	 */
	public static void queryPostingList(final int page, final String section, final String city,
			final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询帖子");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryPostingList");
		new HandlerThread(className, "queryPostingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist").addParam("orderby", 0)
							.addParam("section", section).addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE).addParam("city", city).removeUserParam()
							.post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询帖子列表<br/>
	 * 
	 */
	public static void queryPostingList(final int page, final int orderby, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询帖子");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryPostingList");
		new HandlerThread(className, "queryPostingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist").addParam("orderby", orderby)
							.addParam("section", null).addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE).addParam("city", Constant.city)
							.removeUserParam().post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询我的帖子<br/>
	 * 
	 */
	public static void queryPostingList(final int page, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询帖子");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryPostingList");
		new HandlerThread(className, "queryPostingList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingslist").addParam("orderby", 0)
							.addParam("pageindex", page).addParam("pagesize", SoftwareConstant.PAGER_SIZE).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<Posting> list = JSONArray.parseArray(jo.getString("rows"), Posting.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询帖子详情<br/>
	 * 
	 */
	public static void queryPostingDetail(final Posting item, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询帖子");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryPostingDetail");
		new HandlerThread(className, "queryPostingDetail") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getpostingsdetail").addParam("postingsid", item.getPostingsid())
							.post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					JSONObject jo_old = (JSONObject) JSON.toJSON(item);
					JSONObject jo_parent = new JSONObject();
					jo_parent.putAll(jo_old);
					jo_parent.putAll(jo);
					Posting object = JSON.toJavaObject(jo_parent, Posting.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, object);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 浏览<br/>
	 * type:[1,2]表示[帖子，招聘求职]<br/>
	 * 
	 */
	public static void read(final String id, final int type, final FunctionView<?> functionView,
			final HttpListener listener) {
		functionView.setProgressBarText("正在查询浏览量");
		final HttpHandler handler = getHandler(functionView, listener, className, "read");
		new HandlerThread(className, "read") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("sendview").addParam("id", id).addParam("type", type).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, MessageConstant.MSG_EMPTY);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

}
